package com.incross.dawin.util;

import java.text.DecimalFormat;
import java.util.ArrayList;

import android.content.res.Configuration;
import android.graphics.Bitmap;

import com.incross.dawin.http.parser.AdditionalInfoParser.TickerAd;
import com.incross.dawin.util.CommonUtils.ScreenSize;


public class TickerAdBuilder {
	private ArrayList<TickerAd> mTickerList;
	private ArrayList<Bitmap> mBitmapList;
	
	public TickerAdBuilder(ArrayList<TickerAd> list, ArrayList<Bitmap> bitmap) {
		mTickerList = list;
		mBitmapList = bitmap;
	}
	
	public Ticker build(int orientation, ScreenSize size) {
		if(mTickerList == null || mBitmapList == null || size == null) return null;
		Ticker ticker = null;
		
		ArrayList<TickerInfo> tickerList = new ArrayList<TickerInfo>();
		for(int i = 0; i < mTickerList.size(); i++) {
			TickerAd ad = mTickerList.get(i);
			TickerInfo tickerInfo = null;
			if(mBitmapList.get(i) != null && mTickerList.get(i) != null) {
				if(orientation == Configuration.ORIENTATION_LANDSCAPE || orientation == Configuration.ORIENTATION_PORTRAIT) {
					tickerInfo = new TickerInfo(orientation, size.getWidth(), size.getHeight(), mBitmapList.get(i), mTickerList.get(i));
					tickerList.add(tickerInfo);
				}
			}
		}
		
		if(tickerList.size() > 0) {
			ticker = new Ticker(orientation, tickerList, size);
		}
		
		return ticker;
	}
	
	
	public class Ticker {
		private boolean isLandscapeReady = false;
		private boolean isPortraitReady = false;
		
		private int portParentWidth = -1;
		private int portParentHeight = -1;
		private int landParentWidth = -1;
		private int landParentHeight = -1;
		
		private ArrayList<TickerInfo> tickerList;
		
		public Ticker(int orientation, ArrayList<TickerInfo> _tickerList, ScreenSize size) {
			if(orientation == Configuration.ORIENTATION_PORTRAIT) {
				tickerList = _tickerList;
				isPortraitReady = true;
				portParentWidth = size.getWidth();
				portParentHeight = size.getHeight();
			} else if(orientation == Configuration.ORIENTATION_LANDSCAPE) {
				tickerList = _tickerList;
				isLandscapeReady = true;
				landParentWidth = size.getWidth();
				landParentHeight = size.getHeight();
			}
		}
		
		public boolean setScreenSize(int orientation, ScreenSize size) {
			if(orientation == Configuration.ORIENTATION_PORTRAIT) {
				if(!isPortraitReady) {
					setSize(orientation, size);
					isPortraitReady = true;
				}
			} else if(orientation == Configuration.ORIENTATION_LANDSCAPE) {
				if(!isLandscapeReady) {
					setSize(orientation, size);
					isLandscapeReady = true;
				}
			}
			
			return false;
		}
		
		private void setSize(int orientation, ScreenSize size) {
			if(tickerList != null) {
				if(orientation == Configuration.ORIENTATION_PORTRAIT) {
					for(int i = 0; i < tickerList.size(); i++) {
						if(!tickerList.get(i).isParentSizeAvailable(orientation)) {
							tickerList.get(i).setParentSize(orientation, size);
						}
					}
					
					
				} else if(orientation == Configuration.ORIENTATION_LANDSCAPE) {
					for(int i = 0; i < tickerList.size(); i++) {
						if(!tickerList.get(i).isParentSizeAvailable(orientation)) {
							tickerList.get(i).setParentSize(orientation, size);
						}
					}
				}
			}
		}
		
		public ArrayList<TickerInfo> getTickerList() {
			return tickerList;
		}
		
		public boolean isReady(int orientation) {
			if(orientation == Configuration.ORIENTATION_PORTRAIT) {
				return isPortraitReady;
			} else if(orientation == Configuration.ORIENTATION_LANDSCAPE) {
				return isLandscapeReady;
			}
			return false;
		}
		
		public ScreenSize getScreenSize(int orientation) {
			if(orientation == Configuration.ORIENTATION_LANDSCAPE) {
				if(landParentWidth > 0 && landParentHeight > 0) {
					return new ScreenSize(landParentWidth, landParentHeight);
				}
			} else if(orientation == Configuration.ORIENTATION_PORTRAIT) {
				if(portParentWidth > 0 && portParentHeight > 0) {
					return new ScreenSize(portParentWidth, portParentHeight);
				}
			}
			return null;
		}
	}
	
	public class TickerInfo {
		private double imageRatio = -1;
		
		private int portParentWidth = -1;
		private int portParentHeight = -1;
		private int landParentWidth = -1;
		private int landParentHeight = -1;
		
		private int bitmapWidth = -1;
		private int bitmapHeight = -1;
		
		private int portAdjustedWidth = 0;
		private int portAdjustedHeight = 0;
		private int landAdjustedWidth = 0;
		private int landAdjustedHeight = 0;
		
		private boolean isPortSizeFixed = false;
		private boolean isLandSizeFixed = false;
		
		private Bitmap image = null;
		private TickerAd tickerAd = null;
		
		
		public TickerInfo(int orientation, int _parentWidth, int _parentHeight, Bitmap _image, TickerAd _tickerAd) {
			if(orientation == Configuration.ORIENTATION_PORTRAIT) {
				portParentWidth = _parentWidth;
				portParentHeight = _parentHeight;
			} else {
				landParentWidth = _parentWidth;
				landParentHeight = _parentHeight;
			}
			tickerAd = _tickerAd;
			image = _image;
			
			if(image != null) {
				bitmapWidth = image.getWidth();
				bitmapHeight = image.getHeight();
				String pattern = "#.##";
				DecimalFormat dFormat = new DecimalFormat(pattern);
				double size = (double) tickerAd.getSizePercent() / 100.0;
				imageRatio = Double.parseDouble(dFormat.format(((double) tickerAd.getSizePercent() / 100.0)));
				BKLog.e("######################################  Image ratio : " + imageRatio  + ", sizepercent : " + tickerAd.getSizePercent() + ", 50/100 = " + (50/100));
				setTickerSize(orientation);
			}
		}
		
		private void setTickerSize(int orientation) {
			BKLog.e("-- set Ticker size - TickerInfo class");
			// 이미지 값에 현재 비율을 나누어 버튼 크기 계산
			BKLog.e("image ratio : " + imageRatio + ", bitmap width : " + bitmapWidth + ", bitmapHeight : " + bitmapHeight);
			if(orientation == Configuration.ORIENTATION_PORTRAIT && !isPortSizeFixed) {
				portAdjustedWidth = (int) Math.round((int) portParentWidth * imageRatio);
				portAdjustedHeight = Math.round((int) (portAdjustedWidth * bitmapHeight) / bitmapWidth);
				isPortSizeFixed = true;
				BKLog.e("portparentwidth : " + portParentWidth + ", portParentHeight = " + portParentHeight);
				BKLog.e("portAdjustedWidth : " + portAdjustedWidth + ", portAdjustedHeight : " + portAdjustedHeight);
			} else if(orientation == Configuration.ORIENTATION_LANDSCAPE && !isLandSizeFixed) {
				landAdjustedWidth = (int) Math.round((int) landParentWidth * imageRatio);
				landAdjustedHeight = Math.round((int) (landAdjustedWidth * bitmapHeight) / bitmapWidth);
				isLandSizeFixed = true;
				BKLog.e("landParentwidth : " + landParentWidth + ", landParentHeight = " + landParentHeight);
				BKLog.e("landAdjustedWidth : " + landAdjustedWidth + ", landAdjustedHeight : " + landAdjustedHeight);
			}
		}
		
		public boolean isParentSizeAvailable(int orientation) {
			if(orientation == Configuration.ORIENTATION_LANDSCAPE) {
				if(landParentHeight > 0 && landParentWidth > 0) return true;
			} else if(orientation == Configuration.ORIENTATION_PORTRAIT) {
				if(portParentHeight > 0 && portParentWidth > 0) return true;
			}
			return false;
		}
		
		public void setParentSize(int orientation, ScreenSize size) {
			if(orientation == Configuration.ORIENTATION_PORTRAIT) {
				if(!isPortSizeFixed) {
					portParentWidth = size.getWidth();
					portParentHeight = size.getHeight();
					setTickerSize(orientation);
				}
			} else if(orientation == Configuration.ORIENTATION_LANDSCAPE) {
				if(!isLandSizeFixed) {
					landParentWidth = size.getWidth();
					landParentHeight = size.getHeight();
					setTickerSize(orientation);
				}
			}
		}
		
		public double getImageRatio() {
			return imageRatio;
		}
		
		public int getOriginalWidth() {
			return bitmapWidth;
		}
		
		public int getOriginalHeight() {
			return bitmapHeight;
		}
		
		public int getWidth(int orientation) {
			if(orientation == Configuration.ORIENTATION_PORTRAIT) {
				return portAdjustedWidth;
			} else {
				return landAdjustedWidth;
			}
		}
		
		public int getHeight(int orientation) {
			if(orientation == Configuration.ORIENTATION_PORTRAIT) {
				return portAdjustedHeight;
			} else {
				return landAdjustedHeight;
			}
		}
		
		public Bitmap getBitmap() {
			return image;
		}
		
		public boolean isParentSizeFixed(int orientation) {
			if(orientation == Configuration.ORIENTATION_PORTRAIT) {
				return isPortSizeFixed;
			} else if(orientation == Configuration.ORIENTATION_LANDSCAPE) {
				return isLandSizeFixed;
			}
			return false;
		}
		
		// get ticker ad informations
		public String getProgram() {
			return tickerAd.getProgram();
		}
		
		public int getPosition() {
			return tickerAd.getPosition();
		}
		
		public int getSizePercent() {
			return tickerAd.getSizePercent();
		}
		
		public int getStartViewTime() {
			return tickerAd.getStartViewTime();
		}
		
		public int getEndViewTime() {
			return tickerAd.getEndViewTime();
		}
		
		public String getClickThrough() {
			return tickerAd.getClickThrough();
		}
		
		public String getClickTracking() {
			return tickerAd.getClickTracking();
		}
	}

}
